package cn.edu.xmu.orderpayment.order.controller;

import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.orderpayment.order.model.vo.OrderMessageVo;
import cn.edu.xmu.orderpayment.order.model.vo.ShipmentVo;
import cn.edu.xmu.orderpayment.order.service.CustomerOrderService;
import cn.edu.xmu.orderpayment.order.service.ShopOrderService;
import cn.edu.xmu.privilegegateway.annotation.aop.Audit;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginName;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginUser;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

import java.time.ZonedDateTime;

import static cn.edu.xmu.oomall.core.util.Common.decorateReturnObject;

/**
 * @author Chencheng Liao 22920192204238
 * @date 2021/12/11
 */
@Api(value = "供店铺和管理员访问的订单api")
@Validated
@RefreshScope
@RestController
@RequestMapping(value = "/", produces = "application/json;charset=UTF-8")
public class ShopOrderController {

    @Autowired
    private HttpServletResponse httpServletResponse;

    @Autowired
    private CustomerOrderService customerOrderService;

    @Autowired
    private ShopOrderService shopOrderService;

    /**
     * 店家对订单标记发货
     *
     * @param
     * @return Object
     * @author Chencheng Liao 22920192204238
     * @Date 2021/12/12
     */
    @ApiOperation(value = "店家对订单标记发货", tags = "order")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "用户Token", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "id", value = "订单id", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "shopId", value = "商店id", required = true),
            @ApiImplicitParam(paramType = "body", dataType = "OrderVo", name = "body", value = "指定发货资讯", required = true)})
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "成功"),
            @ApiResponse(code = 507, message = "当前状态禁止此操作")})
    @PutMapping(value = "/shops/{shopId}/orders/{id}/deliver")
    @Audit(departName = "shops")
    public Object deliver(@PathVariable Long shopId, @PathVariable Long id, @LoginUser Long loginUser, @LoginName String loginUserName, @RequestBody ShipmentVo shipmentVo) {
        ReturnObject res = shopOrderService.deliver(loginUser, shopId, id, shipmentVo.getShipmentSn(), loginUserName);
        return decorateReturnObject(res);
    }

    /**
     * 店家修改订单 (留言)
     * @param shopId: 商户id
     * @param id: 订单id
     * @return Object
     * @author Chencheng Liao 22920192204238
     * @Date 2021/12/16
     */
    @ApiOperation(value = "店家修改订单 (留言)", notes="- 需要登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name="authorization", value="用户token", required = true, dataType="String", paramType="header"),
            @ApiImplicitParam(paramType="path", dataType="Long", name="shopId", value = "商户id (店员只能查询本商铺)", required = true),
            @ApiImplicitParam(name="id", required = true, value = "订单id",dataType="Long", paramType="path")
    })
    @ApiResponses({
            @ApiResponse(code = 0, message = "成功")
    })
    @Audit
    @PutMapping("/shops/{shopId}/orders/{id}")
    public Object shopsShopIdOrdersIdPut(@PathVariable Long shopId, @PathVariable Long id, @RequestBody OrderMessageVo vo,@LoginUser Long loginUserId, @LoginName String loginName){
        ReturnObject returnObject=shopOrderService.shopModifyUserOrder(loginUserId, shopId, id, loginName,vo);
        return Common.decorateReturnObject(returnObject);
    }

    /**
     * 店家查询商户所有订单 (概要)
     *
     * @return Object
     * @author Chencheng Liao 22920192204238
     * @Date 2021/12/16
     */
    @ApiOperation(value = "店家查询商户所有订单 (概要)", produces = "application/json", notes = "- 需要登录 - 已经被客户逻辑删除的订单也一并返回。")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "用户token", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "Long", name = "shopId", value = "商户id (店员只能查询本商铺)", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Long", name = "customerId", value = "查询的购买者用户id", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "orderSn", value = "订单编号", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "beginTime", value = "开始时间", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "endTime", value = "结束时间", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "page", value = "页码", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "pageSize", value = "每页数目", required = false)
    })
    @ApiResponses({
            @ApiResponse(code = 0, message = "成功")
    })
    @Audit
    @GetMapping("/shops/{shopId}/orders")
    public Object shopsShopIdOrdersGet(@PathVariable(required = true) Long shopId,
                                       @RequestParam(required = false) Long customerId,
                                       @RequestParam(required = false) String orderSn,
                                       @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime beginTime,
                                       @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime endTime,
                                       @RequestParam(required = false, defaultValue = "1") Integer page,
                                       @RequestParam(required = false, defaultValue = "10") Integer pageSize) {

        if (beginTime != null && endTime != null && beginTime.isAfter(endTime)) {
            ReturnObject returnObjectNotValid = new ReturnObject(ReturnNo.LATE_BEGINTIME);
            return decorateReturnObject(returnObjectNotValid);
        }
        ReturnObject ret = shopOrderService.shopsShopIdOrdersGet(shopId, customerId, orderSn, beginTime, endTime, page, pageSize);
        return Common.decorateReturnObject(ret);
    }

    @ApiOperation(value = "店家查询店内订单完整信息（普通，团购，预售）")
    @GetMapping("shops/{shopId}/orders/{id}")
    @Audit(departName = "shops")
    public Object getShopOrderDetail(@PathVariable Long shopId, @PathVariable Long id) {

        ReturnObject res = shopOrderService.getShopOrderDetail(shopId, id);
        return decorateReturnObject(res);
    }

    /**
     * 管理员取消本店铺订单
     * @param id: 订单id
     * @param shopId: 商户id
     * @return Object
     * @author Chencheng Liao 22920192204238
     * @Date 2021/12/17
     */
//    @ApiOperation(value = "管理员取消本店铺订单",notes = "- 需要登录")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name="authorization", value="用户token", required = true, dataType="String", paramType="header"),
//            @ApiImplicitParam(name="shopId", required = true, value = "商户id (店员只能查询本商铺)",dataType="Long", paramType="path"),
//            @ApiImplicitParam(name="id", required = true, value = "订单id",dataType="Long", paramType="path")
//    })
//    @ApiResponses({
//            @ApiResponse(code = 800, message = "订单状态禁止"),
//            @ApiResponse(code = 0, message = "成功")
//    })
//    @Audit
//    @DeleteMapping("/shops/{shopId}/orders/{id}")
//    public Object shopsShopIdOrdersIdDelete(@PathVariable Long shopId, @PathVariable Long id){
//        ReturnObject returnObject=shopOrderService.shopsShopIdOrdersIdDelete(shopId,id);
//        return Common.decorateReturnObject(returnObject);
//    }

}
